// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.scripting.objects;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;
import zombie.core.logger.ExceptionLogger;
import zombie.core.math.PZMath;
import zombie.core.skinnedmodel.runtime.RuntimeAnimationScript;
import zombie.debug.DebugLog;
import zombie.iso.MultiStageBuilding;
import zombie.scripting.IScriptObjectStore;
import zombie.scripting.ScriptManager;
import zombie.scripting.ScriptParser;
import zombie.vehicles.VehicleEngineRPM;

public final class ScriptModule extends BaseScriptObject implements IScriptObjectStore {
    public String name;
    public String value;
    public final HashMap<String, Item> ItemMap = new HashMap();
    public final HashMap<String, GameSoundScript> GameSoundMap = new HashMap();
    public final ArrayList<GameSoundScript> GameSoundList = new ArrayList();
    public final HashMap<String, AnimationsMesh> AnimationsMeshMap = new HashMap();
    public final HashMap<String, MannequinScript> MannequinScriptMap = new HashMap();
    public final TreeMap<String, ModelScript> ModelScriptMap = new TreeMap(String.CASE_INSENSITIVE_ORDER);
    public final HashMap<String, RuntimeAnimationScript> RuntimeAnimationScriptMap = new HashMap();
    public final HashMap<String, SoundTimelineScript> SoundTimelineMap = new HashMap();
    public final HashMap<String, VehicleScript> VehicleMap = new HashMap();
    public final HashMap<String, VehicleTemplate> VehicleTemplateMap = new HashMap();
    public final HashMap<String, VehicleEngineRPM> VehicleEngineRPMMap = new HashMap();
    public final ArrayList<Recipe> RecipeMap = new ArrayList();
    public final HashMap<String, Recipe> RecipeByName = new HashMap();
    public final HashMap<String, Recipe> RecipesWithDotInName = new HashMap();
    public final ArrayList<EvolvedRecipe> EvolvedRecipeMap = new ArrayList();
    public final ArrayList<UniqueRecipe> UniqueRecipeMap = new ArrayList();
    public final HashMap<String, Fixing> FixingMap = new HashMap();
    public final ArrayList<String> Imports = new ArrayList();
    public boolean disabled = false;

    public void Load(String _name, String strArray) {
        this.name = _name;
        this.value = strArray.trim();
        ScriptManager.instance.CurrentLoadingModule = this;
        this.ParseScriptPP(this.value);
        this.ParseScript(this.value);
        this.value = "";
    }

    private String GetTokenType(String string0) {
        int int0 = string0.indexOf(123);
        if (int0 == -1) {
            return null;
        } else {
            String string1 = string0.substring(0, int0).trim();
            int int1 = string1.indexOf(32);
            int int2 = string1.indexOf(9);
            if (int1 != -1 && int2 != -1) {
                return string1.substring(0, PZMath.min(int1, int2));
            } else if (int1 != -1) {
                return string1.substring(0, int1);
            } else {
                return int2 != -1 ? string1.substring(0, int2) : string1;
            }
        }
    }

    private void CreateFromTokenPP(String string0) {
        string0 = string0.trim();
        String string1 = this.GetTokenType(string0);
        if (string1 != null) {
            if ("item".equals(string1)) {
                String[] string2 = string0.split("[{}]");
                String string3 = string2[0];
                string3 = string3.replace("item", "");
                string3 = string3.trim();
                Item item = new Item();
                this.ItemMap.put(string3, item);
            } else if ("animationsMesh".equals(string1)) {
                String[] string4 = string0.split("[{}]");
                String string5 = string4[0];
                string5 = string5.replace("animationsMesh", "");
                string5 = string5.trim();
                if (this.AnimationsMeshMap.containsKey(string5)) {
                    AnimationsMesh animationsMesh0 = (AnimationsMesh)this.AnimationsMeshMap.get(string5);
                    animationsMesh0.reset();
                } else {
                    AnimationsMesh animationsMesh1 = new AnimationsMesh();
                    this.AnimationsMeshMap.put(string5, animationsMesh1);
                }
            } else if ("mannequin".equals(string1)) {
                String[] string6 = string0.split("[{}]");
                String string7 = string6[0];
                string7 = string7.replace("mannequin", "");
                string7 = string7.trim();
                if (this.MannequinScriptMap.containsKey(string7)) {
                    MannequinScript mannequinScript0 = (MannequinScript)this.MannequinScriptMap.get(string7);
                    mannequinScript0.reset();
                } else {
                    MannequinScript mannequinScript1 = new MannequinScript();
                    this.MannequinScriptMap.put(string7, mannequinScript1);
                }
            } else if ("model".equals(string1)) {
                String[] string8 = string0.split("[{}]");
                String string9 = string8[0];
                string9 = string9.replace("model", "");
                string9 = string9.trim();
                if (this.ModelScriptMap.containsKey(string9)) {
                    ModelScript modelScript0 = (ModelScript)this.ModelScriptMap.get(string9);
                    modelScript0.reset();
                } else {
                    ModelScript modelScript1 = new ModelScript();
                    this.ModelScriptMap.put(string9, modelScript1);
                }
            } else if ("sound".equals(string1)) {
                String[] string10 = string0.split("[{}]");
                String string11 = string10[0];
                string11 = string11.replace("sound", "");
                string11 = string11.trim();
                if (this.GameSoundMap.containsKey(string11)) {
                    GameSoundScript gameSoundScript0 = (GameSoundScript)this.GameSoundMap.get(string11);
                    gameSoundScript0.reset();
                } else {
                    GameSoundScript gameSoundScript1 = new GameSoundScript();
                    this.GameSoundMap.put(string11, gameSoundScript1);
                    this.GameSoundList.add(gameSoundScript1);
                }
            } else if ("soundTimeline".equals(string1)) {
                String[] string12 = string0.split("[{}]");
                String string13 = string12[0];
                string13 = string13.replace("soundTimeline", "");
                string13 = string13.trim();
                if (this.SoundTimelineMap.containsKey(string13)) {
                    SoundTimelineScript soundTimelineScript0 = (SoundTimelineScript)this.SoundTimelineMap.get(string13);
                    soundTimelineScript0.reset();
                } else {
                    SoundTimelineScript soundTimelineScript1 = new SoundTimelineScript();
                    this.SoundTimelineMap.put(string13, soundTimelineScript1);
                }
            } else if ("vehicle".equals(string1)) {
                String[] string14 = string0.split("[{}]");
                String string15 = string14[0];
                string15 = string15.replace("vehicle", "");
                string15 = string15.trim();
                VehicleScript vehicleScript = new VehicleScript();
                this.VehicleMap.put(string15, vehicleScript);
            } else if ("template".equals(string1)) {
                String[] string16 = string0.split("[{}]");
                String string17 = string16[0];
                string17 = string17.replace("template", "");
                String[] string18 = string17.trim().split("\\s+");
                if (string18.length == 2) {
                    String string19 = string18[0].trim();
                    String string20 = string18[1].trim();
                    if ("vehicle".equals(string19)) {
                        VehicleTemplate vehicleTemplate = new VehicleTemplate(this, string20, string0);
                        vehicleTemplate.module = this;
                        this.VehicleTemplateMap.put(string20, vehicleTemplate);
                    }
                }
            } else if ("animation".equals(string1)) {
                String[] string21 = string0.split("[{}]");
                String string22 = string21[0];
                string22 = string22.replace("animation", "");
                string22 = string22.trim();
                if (this.RuntimeAnimationScriptMap.containsKey(string22)) {
                    RuntimeAnimationScript runtimeAnimationScript0 = (RuntimeAnimationScript)this.RuntimeAnimationScriptMap.get(string22);
                    runtimeAnimationScript0.reset();
                } else {
                    RuntimeAnimationScript runtimeAnimationScript1 = new RuntimeAnimationScript();
                    this.RuntimeAnimationScriptMap.put(string22, runtimeAnimationScript1);
                }
            } else if ("vehicleEngineRPM".equals(string1)) {
                String[] string23 = string0.split("[{}]");
                String string24 = string23[0];
                string24 = string24.replace("vehicleEngineRPM", "");
                string24 = string24.trim();
                if (this.VehicleEngineRPMMap.containsKey(string24)) {
                    VehicleEngineRPM vehicleEngineRPM0 = (VehicleEngineRPM)this.VehicleEngineRPMMap.get(string24);
                    vehicleEngineRPM0.reset();
                } else {
                    VehicleEngineRPM vehicleEngineRPM1 = new VehicleEngineRPM();
                    this.VehicleEngineRPMMap.put(string24, vehicleEngineRPM1);
                }
            }
        }
    }

    private void CreateFromToken(String string0) {
        string0 = string0.trim();
        String string1 = this.GetTokenType(string0);
        if (string1 != null) {
            if ("imports".equals(string1)) {
                String[] string2 = string0.split("[{}]");
                String[] string3 = string2[1].split(",");

                for (int _int = 0; _int < string3.length; _int++) {
                    if (string3[_int].trim().length() > 0) {
                        String string4 = string3[_int].trim();
                        if (string4.equals(this.getName())) {
                            DebugLog.log("ERROR: module \"" + this.getName() + "\" imports itself");
                        } else {
                            this.Imports.add(string4);
                        }
                    }
                }
            } else if ("item".equals(string1)) {
                String[] string5 = string0.split("[{}]");
                String string6 = string5[0];
                string6 = string6.replace("item", "");
                string6 = string6.trim();
                String[] string7 = string5[1].split(",");
                Item item = (Item)this.ItemMap.get(string6);
                item.module = this;

                try {
                    item.Load(string6, string7);
                } catch (Exception exception0) {
                    DebugLog.log(exception0);
                }
            } else if ("recipe".equals(string1)) {
                String[] string8 = string0.split("[{}]");
                String string9 = string8[0];
                string9 = string9.replace("recipe", "");
                string9 = string9.trim();
                String[] string10 = string8[1].split(",");
                Recipe recipe = new Recipe();
                this.RecipeMap.add(recipe);
                if (!this.RecipeByName.containsKey(string9)) {
                    this.RecipeByName.put(string9, recipe);
                }

                if (string9.contains(".")) {
                    this.RecipesWithDotInName.put(string9, recipe);
                }

                recipe.module = this;
                recipe.Load(string9, string10);
            } else if ("uniquerecipe".equals(string1)) {
                String[] string11 = string0.split("[{}]");
                String string12 = string11[0];
                string12 = string12.replace("uniquerecipe", "");
                string12 = string12.trim();
                String[] string13 = string11[1].split(",");
                UniqueRecipe uniqueRecipe = new UniqueRecipe(string12);
                this.UniqueRecipeMap.add(uniqueRecipe);
                uniqueRecipe.module = this;
                uniqueRecipe.Load(string12, string13);
            } else if ("evolvedrecipe".equals(string1)) {
                String[] string14 = string0.split("[{}]");
                String string15 = string14[0];
                string15 = string15.replace("evolvedrecipe", "");
                string15 = string15.trim();
                String[] string16 = string14[1].split(",");
                boolean _boolean = false;

                for (EvolvedRecipe evolvedRecipe0 : this.EvolvedRecipeMap) {
                    if (evolvedRecipe0.name.equals(string15)) {
                        evolvedRecipe0.Load(string15, string16);
                        evolvedRecipe0.module = this;
                        _boolean = true;
                    }
                }

                if (!_boolean) {
                    EvolvedRecipe evolvedRecipe1 = new EvolvedRecipe(string15);
                    this.EvolvedRecipeMap.add(evolvedRecipe1);
                    evolvedRecipe1.module = this;
                    evolvedRecipe1.Load(string15, string16);
                }
            } else if ("fixing".equals(string1)) {
                String[] string17 = string0.split("[{}]");
                String string18 = string17[0];
                string18 = string18.replace("fixing", "");
                string18 = string18.trim();
                String[] string19 = string17[1].split(",");
                Fixing fixing = new Fixing();
                fixing.module = this;
                this.FixingMap.put(string18, fixing);
                fixing.Load(string18, string19);
            } else if ("animationsMesh".equals(string1)) {
                String[] string20 = string0.split("[{}]");
                String string21 = string20[0];
                string21 = string21.replace("animationsMesh", "");
                string21 = string21.trim();
                AnimationsMesh animationsMesh = (AnimationsMesh)this.AnimationsMeshMap.get(string21);
                animationsMesh.module = this;

                try {
                    animationsMesh.Load(string21, string0);
                } catch (Throwable throwable0) {
                    ExceptionLogger.logException(throwable0);
                }
            } else if ("mannequin".equals(string1)) {
                String[] string22 = string0.split("[{}]");
                String string23 = string22[0];
                string23 = string23.replace("mannequin", "");
                string23 = string23.trim();
                MannequinScript mannequinScript = (MannequinScript)this.MannequinScriptMap.get(string23);
                mannequinScript.module = this;

                try {
                    mannequinScript.Load(string23, string0);
                } catch (Throwable throwable1) {
                    ExceptionLogger.logException(throwable1);
                }
            } else if ("multistagebuild".equals(string1)) {
                String[] string24 = string0.split("[{}]");
                String string25 = string24[0];
                string25 = string25.replace("multistagebuild", "");
                string25 = string25.trim();
                String[] string26 = string24[1].split(",");
                MultiStageBuilding.Stage stage = new MultiStageBuilding().new Stage();
                stage.Load(string25, string26);
                MultiStageBuilding.addStage(stage);
            } else if ("model".equals(string1)) {
                String[] string27 = string0.split("[{}]");
                String string28 = string27[0];
                string28 = string28.replace("model", "");
                string28 = string28.trim();
                ModelScript modelScript = (ModelScript)this.ModelScriptMap.get(string28);
                modelScript.module = this;

                try {
                    modelScript.Load(string28, string0);
                } catch (Throwable throwable2) {
                    ExceptionLogger.logException(throwable2);
                }
            } else if ("sound".equals(string1)) {
                String[] string29 = string0.split("[{}]");
                String string30 = string29[0];
                string30 = string30.replace("sound", "");
                string30 = string30.trim();
                GameSoundScript gameSoundScript = (GameSoundScript)this.GameSoundMap.get(string30);
                gameSoundScript.module = this;

                try {
                    gameSoundScript.Load(string30, string0);
                } catch (Throwable throwable3) {
                    ExceptionLogger.logException(throwable3);
                }
            } else if ("soundTimeline".equals(string1)) {
                String[] string31 = string0.split("[{}]");
                String string32 = string31[0];
                string32 = string32.replace("soundTimeline", "");
                string32 = string32.trim();
                SoundTimelineScript soundTimelineScript = (SoundTimelineScript)this.SoundTimelineMap.get(string32);
                soundTimelineScript.module = this;

                try {
                    soundTimelineScript.Load(string32, string0);
                } catch (Throwable throwable4) {
                    ExceptionLogger.logException(throwable4);
                }
            } else if ("vehicle".equals(string1)) {
                String[] string33 = string0.split("[{}]");
                String string34 = string33[0];
                string34 = string34.replace("vehicle", "");
                string34 = string34.trim();
                VehicleScript vehicleScript = (VehicleScript)this.VehicleMap.get(string34);
                vehicleScript.module = this;

                try {
                    vehicleScript.Load(string34, string0);
                    vehicleScript.Loaded();
                } catch (Exception exception1) {
                    ExceptionLogger.logException(exception1);
                }
            } else if (!"template".equals(string1)) {
                if ("animation".equals(string1)) {
                    String[] string35 = string0.split("[{}]");
                    String string36 = string35[0];
                    string36 = string36.replace("animation", "");
                    string36 = string36.trim();
                    RuntimeAnimationScript runtimeAnimationScript = (RuntimeAnimationScript)this.RuntimeAnimationScriptMap.get(string36);
                    runtimeAnimationScript.module = this;

                    try {
                        runtimeAnimationScript.Load(string36, string0);
                    } catch (Throwable throwable5) {
                        ExceptionLogger.logException(throwable5);
                    }
                } else if ("vehicleEngineRPM".equals(string1)) {
                    String[] string37 = string0.split("[{}]");
                    String string38 = string37[0];
                    string38 = string38.replace("vehicleEngineRPM", "");
                    string38 = string38.trim();
                    VehicleEngineRPM vehicleEngineRPM = (VehicleEngineRPM)this.VehicleEngineRPMMap.get(string38);
                    vehicleEngineRPM.module = this;

                    try {
                        vehicleEngineRPM.Load(string38, string0);
                    } catch (Throwable throwable6) {
                        this.VehicleEngineRPMMap.remove(string38);
                        ExceptionLogger.logException(throwable6);
                    }
                } else {
                    DebugLog.Script.warn("unknown script object \"%s\"", string1);
                }
            }
        }
    }

    public void ParseScript(String totalFile) {
        ArrayList arrayList = ScriptParser.parseTokens(totalFile);

        for (int _int = 0; _int < arrayList.size(); _int++) {
            String string = (String)arrayList.get(_int);
            this.CreateFromToken(string);
        }
    }

    public void ParseScriptPP(String totalFile) {
        ArrayList arrayList = ScriptParser.parseTokens(totalFile);

        for (int _int = 0; _int < arrayList.size(); _int++) {
            String string = (String)arrayList.get(_int);
            this.CreateFromTokenPP(string);
        }
    }

    @Override
    public Item getItem(String _name) {
        if (_name.contains(".")) {
            return ScriptManager.instance.getItem(_name);
        } else if (!this.ItemMap.containsKey(_name)) {
            for (int _int = 0; _int < this.Imports.size(); _int++) {
                String string = (String)this.Imports.get(_int);
                ScriptModule scriptModule = ScriptManager.instance.getModule(string);
                Item item = scriptModule.getItem(_name);
                if (item != null) {
                    return item;
                }
            }

            return null;
        } else {
            return (Item)this.ItemMap.get(_name);
        }
    }

    public ModelScript getModelScript(String _name) {
        if (_name.contains(".")) {
            return ScriptManager.instance.getModelScript(_name);
        } else {
            ModelScript modelScript = (ModelScript)this.ModelScriptMap.get(_name);
            if (modelScript == null) {
                for (int _int = 0; _int < this.Imports.size(); _int++) {
                    String string = (String)this.Imports.get(_int);
                    ScriptModule scriptModule = ScriptManager.instance.getModule(string);
                    modelScript = scriptModule.getModelScript(_name);
                    if (modelScript != null) {
                        return modelScript;
                    }
                }

                return null;
            } else {
                return modelScript;
            }
        }
    }

    @Override
    public Recipe getRecipe(String _name) {
        if (_name.contains(".") && !this.RecipesWithDotInName.containsKey(_name)) {
            return ScriptManager.instance.getRecipe(_name);
        } else {
            Recipe recipe = (Recipe)this.RecipeByName.get(_name);
            if (recipe != null) {
                return recipe;
            } else {
                for (int _int = 0; _int < this.Imports.size(); _int++) {
                    ScriptModule scriptModule = ScriptManager.instance.getModule((String)this.Imports.get(_int));
                    if (scriptModule != null) {
                        recipe = scriptModule.getRecipe(_name);
                        if (recipe != null) {
                            return recipe;
                        }
                    }
                }

                return null;
            }
        }
    }

    public VehicleScript getVehicle(String _name) {
        if (_name.contains(".")) {
            return ScriptManager.instance.getVehicle(_name);
        } else if (!this.VehicleMap.containsKey(_name)) {
            for (int _int = 0; _int < this.Imports.size(); _int++) {
                VehicleScript vehicleScript = ScriptManager.instance.getModule((String)this.Imports.get(_int)).getVehicle(_name);
                if (vehicleScript != null) {
                    return vehicleScript;
                }
            }

            return null;
        } else {
            return (VehicleScript)this.VehicleMap.get(_name);
        }
    }

    public VehicleTemplate getVehicleTemplate(String _name) {
        if (_name.contains(".")) {
            return ScriptManager.instance.getVehicleTemplate(_name);
        } else if (!this.VehicleTemplateMap.containsKey(_name)) {
            for (int _int = 0; _int < this.Imports.size(); _int++) {
                VehicleTemplate vehicleTemplate = ScriptManager.instance.getModule((String)this.Imports.get(_int)).getVehicleTemplate(_name);
                if (vehicleTemplate != null) {
                    return vehicleTemplate;
                }
            }

            return null;
        } else {
            return (VehicleTemplate)this.VehicleTemplateMap.get(_name);
        }
    }

    public VehicleEngineRPM getVehicleEngineRPM(String _name) {
        return _name.contains(".") ? ScriptManager.instance.getVehicleEngineRPM(_name) : (VehicleEngineRPM)this.VehicleEngineRPMMap.get(_name);
    }

    public boolean CheckExitPoints() {
        return false;
    }

    public String getName() {
        return this.name;
    }

    public void Reset() {
        this.ItemMap.clear();
        this.GameSoundMap.clear();
        this.GameSoundList.clear();
        this.AnimationsMeshMap.clear();
        this.MannequinScriptMap.clear();
        this.ModelScriptMap.clear();
        this.RuntimeAnimationScriptMap.clear();
        this.SoundTimelineMap.clear();
        this.VehicleMap.clear();
        this.VehicleTemplateMap.clear();
        this.VehicleEngineRPMMap.clear();
        this.RecipeMap.clear();
        this.RecipeByName.clear();
        this.RecipesWithDotInName.clear();
        this.EvolvedRecipeMap.clear();
        this.UniqueRecipeMap.clear();
        this.FixingMap.clear();
        this.Imports.clear();
    }

    public Item getSpecificItem(String _name) {
        return (Item)this.ItemMap.get(_name);
    }
}
